package com.lys.service.impl;

import com.lys.dao.UserAddressDao;
import com.lys.entity.UserAddress;
import com.lys.param.UserAddressParam;
import com.lys.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by bdqn on 2016/5/12.
 */
@Service
public class UserAddressServiceImpl implements UserAddressService {

    @Autowired
    private UserAddressDao userAddressDao;

    /**
     * 查询用户地址列表
     *
     * @param id
     * @return
     * @throws Exception
     */
    public List<UserAddress> queryUserAdressList(Integer id) throws Exception{
        List<UserAddress> userAddressList = null;
        try {
            UserAddressParam params = new UserAddressParam();
            params.setUserId(id);
            userAddressList = userAddressDao.queryUserAddressList(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userAddressList;
    }
    /**
     * 添加用户地址
     *
     * @param id
     * @param address
     * @return
     */
    @Override
    public Integer addUserAddress(Integer id, String address,String remark) {
        Integer userAddressId = null;
        try {
            UserAddress userAddress=new UserAddress();
            userAddress.setUserId(id);
            userAddress.setAddress(address);
            userAddress.setRemark(remark);
            userAddressId = userAddressDao.saveUserAddress(userAddress);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userAddressId;
    }

    @Override
    public UserAddress getUserAddressById(Integer id) {
        UserAddress userAddress= null;
        try {
            userAddress =  userAddressDao.getUserAddressById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  userAddress;

    }

//    /**
//     * 查询用户地址列表
//     *
//     * @param id
//     * @return
//     * @throws Exception
//     */
//    public List<UserAddress> queryUserAdressList(Integer id) throws Exception{
//        Connection connection = null;
//        List<UserAddress> userAddressList = null;
//        try {
//            connection = DataSourceUtil.openConnection();
//            UserAddressDao userAddressDao = new UserAddressDaoImpl(connection);
//            UserAddressParam params = new UserAddressParam();
//            params.setUserId(id);
//            userAddressList = userAddressDao.queryUserAddressList(params);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }finally{
//        	DataSourceUtil.closeConnection(connection);
//        }
//        return userAddressList;
//    }
//    /**
//     * 添加用户地址
//     *
//     * @param id
//     * @param address
//     * @return
//     */
//    @Override
//    public Integer addUserAddress(Integer id, String address,String remark) {
//        Connection connection = null;
//        Integer userAddressId = null;
//        try {
//            connection = DataSourceUtil.openConnection();
//            UserAddressDao userAddressDao = new UserAddressDaoImpl(connection);
//            UserAddress userAddress=new UserAddress();
//            userAddress.setUserId(id);
//            userAddress.setAddress(address);
//            userAddress.setRemark(remark);
//            userAddressId = userAddressDao.saveUserAddress(userAddress);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }finally{
//        	DataSourceUtil.closeConnection(connection);
//        }
//        return userAddressId;
//    }
//
//    @Override
//    public UserAddress getUserAddressById(Integer id) {
//        Connection connection = null;
//        UserAddress userAddress= null;
//        try {
//            connection = DataSourceUtil.openConnection();
//            UserAddressDao userAddressDao = new UserAddressDaoImpl(connection);
//            userAddress = (UserAddress) userAddressDao.getUserAddressById(id);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }finally {
//            DataSourceUtil.closeConnection(connection);
//            return  userAddress;
//        }
//    }
}
